package com.yzkj.cpjxc.business.controller;

import com.yzkj.cpjxc.business.framework.common.BusinessMap;
import com.yzkj.cpjxc.business.framework.common.CSRFTokenManager;
import com.yzkj.cpjxc.business.framework.common.Constants;
import com.yzkj.cpjxc.business.framework.common.Pager;
import com.yzkj.cpjxc.business.system.interceptor.DoLog;
import com.yzkj.cpjxc.business.system.interceptor.Permission;
import com.yzkj.cpjxc.business.domain.PingZhengChild;
import com.yzkj.cpjxc.business.domain.PingZhengChildQueryVo;
import com.yzkj.cpjxc.business.service.PingZhengChildService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * 凭证管理关联子表控制器
 */
@Controller
@RequestMapping("/pingZhengChild")
public class PingZhengChildController extends BaseController {

	@Autowired
	private PingZhengChildService pingZhengChildService;

	@RequestMapping("/list")
	@DoLog(cnContent = "查看凭证管理关联子表列表信息")
	@Permission(name="pingZhengChild.view")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response, PingZhengChild pingZhengChild) throws Exception {
		HashMap<String, Object> modelMap = new LinkedHashMap<String, Object>();
		modelMap.put("pingZhengChild", pingZhengChild);
		modelMap.put("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
		return new ModelAndView("pingZhengChild/list").addAllObjects(modelMap);
	}

	@RequestMapping("/ajaxPage")
	@DoLog(cnContent = "凭证管理关联子表列表ajax分页")
	@Permission(name="pingZhengChild.view")
	public String ajaxPage(HttpServletRequest request, HttpServletResponse response, PingZhengChildQueryVo queryVo) throws Exception {
		try {
			Pager<PingZhengChild> p = new Pager<PingZhengChild>();
			String currentPage = request.getParameter(Pager.MY_PAGINATION_CURRENT_PAGE);
			p.setCurrentPage(Integer.parseInt(currentPage == null ? "1" : currentPage));
			Pager<PingZhengChild> pager = pingZhengChildService.search(p, queryVo);
			this.genPageJson(response, pager);
			return null;
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	@RequestMapping("/toAdd")
	@DoLog(cnContent = "去到添加凭证管理关联子表页面")
	@Permission(name="pingZhengChild.add")
	public ModelAndView toAdd(HttpServletRequest request, HttpServletResponse response, PingZhengChild pingZhengChild) throws Exception {
		HashMap<String, Object> modelMap = new LinkedHashMap<String, Object>();
		modelMap.put("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
		return new ModelAndView("pingZhengChild/add").addAllObjects(modelMap);
	}

	@RequestMapping("/add")
	@DoLog(cnContent = "添加凭证管理关联子表")
	@Permission(name="pingZhengChild.add")
	public String add(HttpServletRequest request, HttpServletResponse response, PingZhengChild pingZhengChild) throws Exception {
		try {
			BusinessMap bm = pingZhengChildService.create(pingZhengChild);
			return this.ajax(response, bm.getIsSucc() ? Constants.SUCCESS : bm.getAlertMesg());
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	@RequestMapping("/toEdit")
	@DoLog(cnContent = "去到编辑凭证管理关联子表页面")
	@Permission(name="pingZhengChild.edit")
	public ModelAndView toEdit(HttpServletRequest request, HttpServletResponse response, PingZhengChild pingZhengChild) throws Exception {
		PingZhengChild result = pingZhengChildService.getById(pingZhengChild.getUuid());
		HashMap<String, Object> modelMap = new LinkedHashMap<String, Object>();
		modelMap.put("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
		modelMap.put("pingZhengChild", result);
		return new ModelAndView("pingZhengChild/edit").addAllObjects(modelMap);
	}

	@RequestMapping("/edit")
	@DoLog(cnContent = "编辑凭证管理关联子表")
	@Permission(name="pingZhengChild.edit")
	public String edit(HttpServletRequest request, HttpServletResponse response, PingZhengChild pingZhengChild) throws Exception {
		try {
			BusinessMap bm = pingZhengChildService.edit(pingZhengChild);
			return this.ajax(response, bm.getIsSucc() ? Constants.SUCCESS : bm.getAlertMesg());
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	@RequestMapping("/view")
	@DoLog(cnContent = "查看凭证管理关联子表信息")
	@Permission(name="pingZhengChild.view")
	public ModelAndView view(HttpServletRequest request, HttpServletResponse response, PingZhengChild pingZhengChild) throws Exception {
		PingZhengChild result = pingZhengChildService.getById(pingZhengChild.getUuid());
		return new ModelAndView("pingZhengChild/view").addObject("pingZhengChild", result);
	}

	@RequestMapping("/delete")
	@DoLog(cnContent = "删除凭证管理关联子表信息")
	@Permission(name="pingZhengChild.delete")
	public String delete(HttpServletRequest request, HttpServletResponse response, PingZhengChild pingZhengChild) throws Exception {
		try {
			boolean flag = pingZhengChildService.delete(pingZhengChild.getUuid());
			return this.ajax(response, flag ? Constants.SUCCESS : Constants.ERROR);
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

}
